<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>登录页面</h3>
    <form id="form">
        账号：<input type="text" name="username" id=""><br />
        密码：<input type="password" name="pwd" id=""><br />
        <button>登录</button>
        <p class="tip"></p>
    </form>
    <script src="../public/js/ajax.js"></script>
    <script>
        let form = document.querySelector('#form');
        let usernameInp = document.getElementsByName('username')[0];
        let pwdInp = document.getElementsByName('pwd')[0];
        let tip = document.querySelector('.tip')

        form.onsubmit = function(e){
            e.preventDefault();

            // 登录 - 登录接口
            ajax.post('http://localhost/login',`username=${usernameInp.value}&pwd=${pwdInp.value}`,({code,message,key})=>{
                // console.log(data);
                if(code ===200){
                    console.log("登录成功");

                    // 存储数据
                    sessionStorage.setItem('login',key)

                    window.location.href = './list.html';
                }else{
                    console.log(message);
                    tip.innerHTML = message;
                }
            })

        }


    </script>
</body>
</html>